<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      label-width="68px"
    >
      <el-form-item label="柜机名称" prop="cabinetId">
        <el-select
          v-model="queryParams.cabinetId"
          placeholder="请选择柜机名称"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in cabinetList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mc:ad:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mc:ad:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          批量删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="柜机名称" align="center">
        <template slot-scope="scope">
          {{ getCabinetLabel(scope.row.cabinetId) }}
        </template>
      </el-table-column>
      <el-table-column label="资源类型" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mc_ad_resources_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="展示时长" align="center">
        <template slot-scope="scope"> {{ scope.row.displayDuration || 0 }} 秒 </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-button
            :type="scope.row.status == 0 ? 'success' : 'info'"
            size="mini"
            @click="handleStatusChange(scope.row)"
          >
            {{ scope.row.status == 0 ? "正常" : "停用" }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="资源" align="center" prop="resources">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >
            查看资源
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['mc:ad:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
          <el-button
            v-hasPermi="['mc:ad:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广告信息对话框 -->
    <el-dialog
      :title="editForm.id ? '修改广告信息' : '添加广告信息'"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="editForm" :model="editForm" :rules="rules" label-width="100px">
        <el-form-item label="柜机名称" prop="cabinetId">
          <el-select
            v-model="editForm.cabinetId"
            placeholder="请选择柜机名称"
            filterable
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="item in cabinetList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="资源类型" prop="type">
          <el-select
            v-model="editForm.type"
            placeholder="请选择资源类型"
            clearable
            style="width: 100%"
            @change="changeType"
          >
            <el-option
              v-for="item in dict.type.mc_ad_resources_type"
              :key="item.value"
              :label="item.label"
              :value="Number(item.value)"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item v-if="editForm.type" label="资源地址" prop="resources">
          <Image-upload
            v-if="editForm.type == 1"
            v-model="editForm.resources"
            :limit="1"
            :file-size="50"
          />
          <!-- <Video-upload
            v-if="editForm.type == 2"
            v-model="editForm.resources"
            :limit="1"
          /> -->
        </el-form-item>
        <el-form-item label="展示时长" prop="displayDuration">
          <el-input
            v-model="editForm.displayDuration"
            placeholder="请输入展示时长(秒)"
            type="number"
            :min="1"
            maxlength="10"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="editForm.sort" placeholder="请输入排序" type="number" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="editForm.remark"
            type="textarea"
            placeholder="请输入内容"
            maxlength="200"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="资源预览" :visible.sync="previewShow" width="500px">
      <div class="preview-content">
        <Image-preview
          v-if="info.type == 1"
          :src="info.resources"
          :width="300"
          :height="300"
        />
        <video
          v-if="info.type == 2"
          :src="baseUrl + info.resources"
          width="300"
          height="200"
          controls
        />
      </div>
      <div slot="footer">
        <el-button @click="previewShow = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCabinet } from "@/api/mc/cabinet";
import { listAd, getAd, delAd, addAd, updateAd } from "@/api/mc/ad";

export default {
  name: "Ad",
  dicts: ["mc_ad_resources_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告信息表格数据
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cabinetId: null,
      },
      // 表单参数
      editForm: {},
      // 表单校验
      rules: {
        cabinetId: [{ required: true, message: "柜机名称不能为空", trigger: "blur" }],
        type: [
          { required: true, message: "资源类型不能为空", trigger: ["blur", "change"] },
        ],
        resources: [{ required: true, message: "资源地址不能为空", trigger: "blur" }],
        displayDuration: [
          { required: true, message: "展示时长不能为空", trigger: "blur" },
          { pattern: /^[1-9]\d*$/, message: "展示时长必须为正整数", trigger: "blur" },
        ],
      },
      baseUrl: process.env.VUE_APP_BASE_API,
      cabinetList: [], // 柜机列表
      previewShow: false, // 预览弹窗
      info: {}, // 预览信息
    };
  },
  created() {
    this.getCabinetList();
    this.getList();
  },
  methods: {
    /** 查询广告信息列表 */
    getList() {
      this.loading = true;
      listAd(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 查询柜机列表 */
    getCabinetList() {
      listCabinet({ pageNum: 1, pageSize: 10000 }).then((res) => {
        this.cabinetList = res.rows;
      });
    },
    // 查看资源
    handleView(row) {
      this.info = row;
      this.previewShow = true;
    },
    // 改变类型清空资源地址
    changeType() {
      this.editForm.resources = null;
    },
    // 柜机中文
    getCabinetLabel(id) {
      let row = this.cabinetList.find((item) => item.id == id);
      return row ? row.name : "";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.editForm = {
        id: null,
        cabinetId: null,
        status: null,
        resources: null,
        type: 1,
        displayDuration: null,
        sort: null,
        remark: null,
      };
      this.resetForm("editForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    // 状态修改
    handleStatusChange(row) {
      let text = row.status == 0 ? "停用" : "启用";
      this.$modal
        .confirm(`确认要${text}该广告吗?`)
        .then(() => {
          updateAd({
            id: row.id,
            status: row.status == 0 ? 1 : 0,
          }).then(() => {
            this.$modal.msgSuccess(`${text}成功`);
            this.getList();
          });
        })
        .catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getAd(row.id).then((res) => {
        this.editForm = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          let request = this.editForm.id ? updateAd : addAd;
          request(this.editForm).then((res) => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除广告信息编号为"' + ids + '"的数据项？')
        .then(() => {
          delAd(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.preview-content {
  display: flex;
  justify-content: center;
}
</style>
